package com.chen.service.impl;

import com.chen.dao.FlightDao;
import com.chen.entity.FlightEntity;
import com.chen.service.FlightService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/*
 * 在软件架构中，Service层通常指的是服务层，它是应用程序架构中的一个中间层，
 * 位于表示层（用户界面层）和数据访问层（DAO层）之间。
 * */
//业务逻辑层
@Service
public class FlightServiceImpl implements FlightService {
    //业务逻辑层调用数据库访问层获取数据
    private FlightDao flightDao = new FlightDao();

    public List<FlightEntity> getByAll() {
        return flightDao.getByAll();
    }

    public int deleteFlightById(Integer id) {
        return flightDao.deleteFlightById(id);
    }

    public FlightEntity getByIdFlight(Integer id) {
        return flightDao.getByIdFlight(id);
    }

    public int updateFlight(FlightEntity flightEntity) {
        return flightDao.updateFlight(flightEntity);
    }

    public int insertFlight(FlightEntity flightEntity) {
        return flightDao.insertFlight(flightEntity);
    }

    public void strIsEmpty(String str, HttpServletRequest req, HttpServletResponse resp) {
        try {
            if (StringUtils.isEmpty(str)) {
                req.setAttribute("errorMsg", "错误原因：" + str + "为空值");
                req.getRequestDispatcher("error.jsp").forward(req, resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public FlightEntity setEntity(Integer id, String flightId, String company, String departureAirport, String arriveAirport, Date departureTime, Date arriveTime, String model, Float cost) {
        FlightEntity flightEntity = new FlightEntity(id, flightId, company,
                departureAirport, arriveAirport, departureTime,
                arriveTime, model, cost, 0);
        return flightEntity;
    }

    public int updateDeleteById(Integer id) {
        return flightDao.updateDeleteById(id);
    }

    public List<FlightEntity> searchByAll(String departureAirport, String arriveAirport) {
        return flightDao.searchByAll(departureAirport, arriveAirport);
    }
}
